package org.eiahe.hr.salary.controller;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.ehe.common.core.BaseController;
import org.ehe.common.core.domain.R;
import org.ehe.common.core.validate.AddGroup;
import org.ehe.common.core.validate.EditGroup;
import org.ehe.common.log.annotation.Log;
import org.ehe.common.log.enums.BusinessType;
import org.ehe.common.mybatis.core.page.PageQuery;
import org.ehe.common.mybatis.core.page.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.eiahe.hr.salary.domain.bo.BonusCalculationBo;
import org.eiahe.hr.salary.domain.vo.BonusCalculationVo;
import org.eiahe.hr.salary.service.IBonusCalculationService;
import org.ehe.common.excel.utils.ExcelUtil;

import jakarta.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 奖金核算Controller
 *
 * @author zhangnn
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/salary/calculation")
@Slf4j
public class BonusCalculationController extends BaseController {

    private final IBonusCalculationService iBonusCalculationService;

    /**
     * 查询奖金核算列表
     */
    @SaCheckPermission("salary:calculation:list")
    @GetMapping("/list")
    public TableDataInfo<BonusCalculationVo> list(BonusCalculationBo bo, PageQuery pageQuery) {
        return iBonusCalculationService.queryPageList(bo, pageQuery);
    }

    /**
     * 获取奖金核算详细信息
     *
     * @param calculationId 主键
     */
    @SaCheckPermission("salary:calculation:query")
    @GetMapping("/{calculationId}")
    public R<BonusCalculationVo> getInfo(@PathVariable Long calculationId) {
        return R.ok(iBonusCalculationService.queryById(calculationId));
    }

    /**
     * 新增奖金核算
     */
    @SaCheckPermission("salary:calculation:add")
    @Log(title = "奖金核算", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody BonusCalculationBo bo) {
        return toAjax(iBonusCalculationService.insertByBo(bo));
    }

    /**
     * 修改奖金核算
     */
    @SaCheckPermission("salary:calculation:edit")
    @Log(title = "奖金核算", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BonusCalculationBo bo) {
        return toAjax(iBonusCalculationService.updateByBo(bo));
    }

    /**
     * 删除奖金核算
     *
     * @param calculationIds 主键串
     */
    @SaCheckPermission("salary:calculation:remove")
    @Log(title = "奖金核算", businessType = BusinessType.DELETE)
    @DeleteMapping("/{calculationIds}")
    public R<Void> remove(@PathVariable Long[] calculationIds) {
        return toAjax(iBonusCalculationService.deleteWithValidByIds(calculationIds));
    }

    /**
     * 导出奖金核算列表
     */
    @SaCheckPermission("salary:calculation:export")
    @Log(title = "奖金核算", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BonusCalculationBo bo, HttpServletResponse response) {
        List<BonusCalculationVo> list = iBonusCalculationService.queryList(bo);
        ExcelUtil.exportExcel(list, "奖金核算数据", BonusCalculationVo.class, response);
    }

    /**
     * 处理工作流审核通过
     *
     * @param calculationId 核算ID
     */
    @SaCheckPermission("salary:calculation:approve")
    @Log(title = "奖金核算", businessType = BusinessType.UPDATE)
    @PutMapping("/approve/{calculationId}")
    public R<Void> handleWorkflowApprove(@PathVariable Long calculationId) {
        return toAjax(iBonusCalculationService.handleWorkflowApprove(calculationId));
    }

    /**
     * 处理工作流审核拒绝
     *
     * @param calculationId 核算ID
     */
    @SaCheckPermission("salary:calculation:reject")
    @Log(title = "奖金核算", businessType = BusinessType.UPDATE)
    @PutMapping("/reject/{calculationId}")
    public R<Void> handleWorkflowReject(@PathVariable Long calculationId) {
        return toAjax(iBonusCalculationService.handleWorkflowReject(calculationId));
    }

    /**
     * 发放奖金
     *
     * @param calculationId 核算ID
     */
    @SaCheckPermission("salary:calculation:pay")
    @Log(title = "奖金核算", businessType = BusinessType.UPDATE)
    @PutMapping("/pay/{calculationId}")
    public R<Void> payBonus(@PathVariable Long calculationId) {
        return toAjax(iBonusCalculationService.payBonus(calculationId));
    }
}
